﻿Chenxin.UI.CustomerMgr = Chenxin.extend(Chenxin.UI.PageCommon, {
    actionUrl: 'service/CustomerMgr.ashx',
    createGrid: function() {

        this.ViewHouseListGrid = new Chenxin.UI.Pagination({
            actionUrl: "service/viewHouse.ashx",
            pageSize: 5,
            data: { op: "getlist" },
            autoLoad: false,
            render: function(data) {
                document.getElementById('viewlist_table').innerHTML = data;
            }
        });

        /* return new Chenxin.UI.Pagination({
            actionUrl: this.actionUrl + '?op=getlist',
            id: 'customer',
            renderTo: 'customer_table'
        }); */

		return new Chenxin.UI.JsonGrid({
            actionUrl: this.actionUrl + '?op=getlist',
            id: 'customer_grid',
            renderTo: 'customer_table',
			modelId:'CustomerID',
			title:'客户列表',
			scope:this,
			columns:{
				CustomerName:{
					head:'客户姓名',
					width:65,
					sortable:true
				},
				Kind:{
					head:'客户类别',
					width:65,
					sortable:true
				},
				Phone:{
					head:'客户电话',
					width:180,
					sortable:true,
					style:'text-align:left;',
					renderer:function(val,c,row){
						if(val&&row.Mobile){
							return val+'/'+row.Mobile;
						}
						return (val?val:'')+(row.Mobile?row.Mobile:'');
					}
				},
				OprDepart:{
					head:'操作部门',
					width:80,
					sortable:true
				},
				OprPerson:{
					head:'操作人',
					width:80,
					sortable:true
				},
				PubLevel:{
					head:'公开级别',
					width:80,
					sortable:true
				},
				LastTraceDay:{
					head:'最后跟盘时间',
					width:120,
					sortable:true
				},
				Adddate:{
					head:'建档日期',
					width:80,
					sortable:true
				}
			},
			onClick:function(id,el){
				var _tr=$('.tr_tracelist');
				var _id='';
				if(_tr.length){
					_id=_tr.eq(0)[0].id.substr(5);
				}
				_tr.next().css('border-top-width','0px');
				_tr.remove();
				if(_id!=this.currentId){
					this.loadinfo();
				}
			}
			
		});
    },
    createSearch: function() {
        return new Chenxin.UI.CustomerSearch({
            scope: this
        });
    },
    createForm: function() {
        this.frmViewHouse = new Chenxin.UI.ViewHouseForm({
            scope: this,
            alwaysCheckPermit: true,
            beforeFitData: function(model) {
                this.frmViewHouse.cmbviewuser.param.fatherid = model.DepartId;
                this.frmViewHouse.cmbviewuser.reload(model.ReportorId);
            }
        });
		
        $("#jqmDemand").jqm({ trigger: false }).jqDrag('.jqDrag');
        return new Chenxin.UI.CustomerForm({
            scope: this,
            alwaysCheckPermit: true,
			showTraceWin: function() {
					var _this = this;
					this.scope.createTraceWin.call(this.scope);
					this.scope.currentTraceWin.show();
					$('#tr_noticetime').hide();
					$('#tr_noticecontent').hide();
					var _data = {};

					Chenxin.apply(_data, this.currentCustomerInfo);
					_data.HouseCode = this.currentCustomerInfo.CustomerCode;
					_data.AddPerson = this.currentCustomerInfo.OprPerson;
					_data.AddDepart = this.currentCustomerInfo.OprDepart;
					this.scope.currentTraceWin.fitData(_data);
					this.scope.currentTraceWin.setTitle('客源跟单');
			},
            saveSucc: function() {
                this.reloadGrid();
            },
            afterGetFormJson: function(json) {
                Chenxin.applyIf(json, this.currentModel);
            }
        });

    },
    loadinfo: function() {
        var _this = this;
        Chenxin.ajax({
            type: "get",
            dataType: "text",
            paramType: 'json',
            url: this.actionUrl,
            data: { op: 'getinfo', modelid: this.currentId },
            onSuccess: function(data, msg) {
                if (data.model) {
                    _this.currentCustomerInfo = data.model;
                    _this.renderCustomerInfo();
                }
                _this.createTraceList(data.countTrace);
            }
        });
    },
    renderCustomerInfo: function() {
        var infoStr = '';
        infoStr += '<dl id="info_'+this.currentId+'" class="tr_tracelist" style="display:none;"><dd class="gendan_box" >';
        infoStr += '<div class=" fl">';
        infoStr += '<ul class="padding5">';
        infoStr += '<li style="float:left;width:140px;">';
        infoStr += '<input name="btn_trace" type="button" class="btn_pon_4 magbotm5" value="我要跟单"/>';
        infoStr += '<input id="btn_viewownerinfo" type="button" class="btn_pon_4" value="查看客户信息"/>';
        infoStr += '<input type="button" id="btnEditBaseInfo"  class="btn_pon_4" value="编辑客户信息" />';
        //房源客户
        if (this.currentCustomerInfo.CustomerKind == 10) {
            infoStr += '<input type="button" id="btnToHouse" class="btn_pon_4" value="查看房源" />';
        }
        //客需客户
        else if (this.currentCustomerInfo.CustomerKind == 20) {
            infoStr += '<input type="button" id="btnPubCustomer"  class="btn_pon_4" value="变更公开级别" />';
            if (this.currentCustomerInfo.PubLevel != 0) {
                infoStr += '<input type="button" id="btn_ShiftPubCustomer"  class="btn_pon_4" value="转移公开客源" />';
            }
            infoStr += '<input type="button" id="btnToDemand" class="btn_pon_4" value="查看客需" />';
        }
        //带看客户
        else if (this.currentCustomerInfo.CustomerKind == 30) {
            infoStr += '<input type="button" id="btnToViewHouse" class="btn_pon_4" value="查看带看" />';
        }

        infoStr += '</li>';
        infoStr += '</ul>';
        infoStr += '</div>';
        infoStr += '<div class="gdtab" style="height:180px;"><div class="outborder">';
        infoStr += '<div class="infos intitss" style="width:660px">';
        infoStr += '<span class="rb" style="width:380px"><b>跟单内容</b></span>';
        infoStr += '<span class="rbpsn" style="width:120px"><b>跟单人</b></span>';
        infoStr += '<span class="rbtim" style="width:120px"><b>跟单时间</b></span>';
        infoStr += '</div>';
        infoStr += '<div id="housetrace_table"></div>';
        infoStr += '</div><div id="customertrace_page" style="width:670px"></div></div></dt></dl>';
		$('#' + this.currentId).next().css('border-top-width','1px');
        $('#' + this.currentId).after(infoStr);
    },
    createTraceList: function(count) {
        if (!this.tracePage) {
            this.tracePage = new Chenxin.UI.Pagination({
                domId: 'customertrace_page',
                actionUrl: 'service/customerMsgr.ashx?op=getcustomertracelist',
                data: { customerid: this.currentCustomerInfo.CustomerId },
                render: function(data) {
                    document.getElementById('housetrace_table').innerHTML = data;
                }
            });
        } else {
            this.tracePage.recordCount = 0;
            this.tracePage.hasCount = false;
            this.tracePage.reload({ customerid: this.currentCustomerInfo.CustomerId });
        }
		$('.tr_tracelist').slideDown('fast');
    },
    showCurrentTraceWin: function() {
        var _this = this;
		_this.createTraceWin();
        this.currentTraceWin.show();
        var _data = {};
        Chenxin.apply(_data, this.currentCustomerInfo);
        _data.HouseCode = this.currentCustomerInfo.CustomerCode;
        _data.AddPerson = this.currentCustomerInfo.OprPerson;
        _data.AddDepart = this.currentCustomerInfo.OprDepart;
        this.currentTraceWin.fitData(_data);
        this.currentTraceWin.setTitle('客源跟单');
    },
	createTraceWin:function() {
			_this = this;
		    if (!this.currentTraceWin) {
            this.currentTraceWin = new Chenxin.UI.currentCustomerTraceForm({
                scrop: this,
                afterGetFormJson: function(json) {
                    delete json.AddPerson;
                    delete json.AddDepart;
                    json.HouseAddress = json.Address;
                    json.HouseId = _this.currentCustomerInfo.CustomerId;
                    json.HouseCode = _this.currentCustomerInfo.CustomerCode;
                    json.HouseAddress = _this.currentCustomerInfo.CustomerName;
                    json.status = 1;
                    delete json.Address;
                    delete json.CustomerCode;

                    if (this.isNotice) {
                        this.additionalParam.noticemodel = {
                            body: json.body,
                            NoticeTime: json.SendDate,
                            HouseId: _this.currentId,
                            HouseAddress: _this.currentCustomerInfo.CustomerName
                        };
                        delete json.body;
                        delete json.SendDate;
                    } else {
                        this.additionalParam = {};
                    }
                    this.additionalParam.isnotice = json.isnotice || '0';
                    delete json.isnotice;
                }
            });
        }
	},
    showPubCustomer: function() {
        var _this = this;

        if (!this.PubCustomerForm) {
            this.PubCustomerForm = new Chenxin.UI.PubCustomerForm({
                scrop: _this,
                alwaysCheckPermit: true,
                beforeSave: function() {
                    alert("客户的客需信息将公开到相应的级别!");
                    return true;
                },
                saveSucc: function() {
                    _this.grid.reload();
                }
            });
        }
        this.PubCustomerForm.op = "setpublevel";
        if (this.PubCustomerForm.show(this.PubCustomerForm.op, { currentId: _this.currentId })) {
            this.PubCustomerForm.reset();
            var _data = this.currentCustomerInfo;
            this.PubCustomerForm.fitData(_data);
            this.PubCustomerForm.setTitle('变更公开级别');
        }
    },
    showShiftPubCustomer: function() {
        var _this = this;
        if (!this.ShiftPubCustomerForm) {
            this.ShiftPubCustomerForm = new Chenxin.UI.ShiftPubCustomerForm({
                scrop: _this,
                alwaysCheckPermit: true,
                beforeFitData: function(model) {
                    if (model.CityId) {
                        _this.ShiftPubCustomerForm.city.reload(model.CityId);
                        _this.ShiftPubCustomerForm.company.param.fatherid = model.CityId;
                        _this.ShiftPubCustomerForm.company.reload(model.CompanyId);
                        //_this.ShiftPubCustomerForm.departs.param.fatherid = model.CompanyId;
                        _this.ShiftPubCustomerForm.departs.reload();
                        //_this.ShiftPubCustomerForm.emp.param.fatherid = model.OprDepartId;
                        _this.ShiftPubCustomerForm.emp.reload();
                    }
                },
                saveSucc: function() {
                    _this.grid.reload();
                }
            });
        }
        this.ShiftPubCustomerForm.op = "shiftpubcustomer";
        if (this.ShiftPubCustomerForm.show(this.ShiftPubCustomerForm.op, { currentId: _this.currentId })) {
            this.ShiftPubCustomerForm.reset();
            var _data = this.currentCustomerInfo;
            this.ShiftPubCustomerForm.fitData(_data);
            this.ShiftPubCustomerForm.setTitle('转移公开客源');
        }
    },
    initEvent: function() {
        var _this = this;
        $('#btnEditBaseInfo').live('click', function() {
            _this.form.permitParam = { currentId: _this.currentId };
            _this.edit();
            _this.form.setTitle('编辑客户信息');
        });

        $('#btn_viewownerinfo').live('click', function() {
            _this.form.currentCustomerInfo = _this.currentCustomerInfo;
            _this.form.permitParam = { currentId: _this.currentId };
            _this.view();
            _this.form.setTitle('查看客户');

        });
        $("input[name='btn_trace']").live('click', function() {
            _this.showCurrentTraceWin();
        });

        $('#btnPubCustomer').live('click', function() {
            _this.showPubCustomer();
        });

        $('#btn_ShiftPubCustomer').live('click', function() {
            _this.showShiftPubCustomer();
        });

        //查看房源
        $("#btnToHouse").live("click", function() {
            Chenxin.ajax({
                type: "get",
                dataType: "text",
                paramType: 'json',
                url: '/service/housemgr.ashx',
                data: { op: 'gethouseid', CustomerId: _this.currentId },
                onSuccess: function(data, msg) {
                    if (data != "") {
                        window.open("/ShowDetailInfo.aspx?houseid=" + data);
                        //window.location.href = "/ShowDetailInfo.aspx?houseid=" + data;
                    } else {
                        alert("数据有误");
                    }
                }
            });
        });

        //查看客需
        $("#btnToDemand").live("click", function() {
            Chenxin.ajax({
                type: "get",
                dataType: "text",
                paramType: 'json',
                url: '/service/customerMsgr.ashx',
                data: { op: 'getdemand', CustomerId: _this.currentId },
                onSuccess: function(data, msg) {
                    $("#divDemand").html(data);
                    $("#jqmDemand").jqmShow();
                }
            });
        });

        //查看带看
        $("#btnToViewHouse").live("click", function() {

            var viewHouseId;
            Chenxin.ajax({
                type: "get",
                dataType: "text",
                paramType: 'json',
                async: false,
                url: '/service/viewHouse.ashx',
                data: { op: 'getmodelid', CustomerId: _this.currentId },
                onSuccess: function(data) {
                    viewHouseId = data;
                }
            });

            _this.frmViewHouse.permitParam = { currentId: viewHouseId };
            if (_this.frmViewHouse.show()) {
                _this.getModel(viewHouseId, _this.frmViewHouse.fitData, _this.frmViewHouse, "/service/viewHouse.ashx");
                _this.frmViewHouse.setDisable();
            }
            _this.frmViewHouse.setTitle('查看带看单');
            _this.ViewHouseListGrid.reload({ op: "getlist", RecordId: viewHouseId, type: "detail" });

        });
    },
    initComponent: function() {
        Chenxin.UI.CustomerMgr.superclass.initComponent.call(this);
    }
});

Chenxin.UI.CustomerSearch = Chenxin.extend(Chenxin.UI.SearchCommon, {
    id: 'customer_search',
    initEvent: function() {
        var _this = this;
        $('#btn_search').live('click', function() {
            _this.scope.reloadGrid();
        });
    },
    createComboList: function() {
        var combl = new Chenxin.UI.StructComboList();
        var structs = combl.comboList;
        var customerkind = new Chenxin.UI.Combo({
            containerId: '#txtCustomerKind',
            name: 'txtCustomerKind',
            tbxWidth: 45,
           // defVal: '10',
            //  defTxt: '客源客户',
            items: comboCommon.CustomerKind
        });
        
        var customerpublevel = new Chenxin.UI.Combo({
            containerId: '#txtCustomerPubLevel',
            name: 'txtCustomerPubLevel',
            tbxWidth: 80,
            colWidth: 90,
            items: comboCommon.CustomerPubLevel
        });
        
        structs.push(customerkind);
        structs.push(customerpublevel);
        return structs;
    },
    initComponent: function() {
        Chenxin.UI.CustomerSearch.superclass.initComponent.call(this);
    }
});

/**
*@description 带看表单
*/
Chenxin.UI.ViewHouseForm = Chenxin.extend(Chenxin.UI.FormCommon, {
    actionUrl: 'service/viewHouse.ashx',
    id: 'jqmviewhouse',
    renderTo: 'divviewhouse',
    autoHide: true,
    jqmWidth: 700,
	    initFormValidator: function() {
        var _this = this;
        //客户名
        $("#CustomerName").formValidator({
            validatorgroup: this.id,
            onshow: "请输入用户姓名",
            onfocus: "请输入用户姓名",
            oncorrect: " "
        }).inputValidator({
            min: 1,
            empty: { leftempty: false, rightempty: false, emptyerror: "用户姓名不能为空" },
            onerror: "用户姓名不能为空"
        });
        //带看时间
        $("#ViewTime").focus(function() {
            WdatePicker({ oncleared: function() { $(this).blur(); }, onpicked: function() { $(this).blur(); } })
        }).formValidator({ validatorgroup: this.id,
            onshow: "请输入带看时间",
            onfocus: "请输入带看时间",
            oncorrect: " "
        }).inputValidator({
            empty: { leftempty: false, rightempty: false, emptyerror: "带看时间不能为空" },
            type: "date",
            onerror: "请输入带看时间"
        });
        //证件号
        $("#CertificateNo").formValidator({
            validatorgroup: this.id,
            onshow: "请输入证件号",
            onfocus: "请输入证件号",
            oncorrect: " "
        }).functionValidator({
            fun: function(val, elem) {
                if (_this.comboList[0].getText() == '身份证') {
                    return isCardID(val);
                } else {
                    return true; //val.isBlank();
                }
            },
            oncorrect: " ",
            onshow: " ",
            onerror: "请输入正确的证件号"
        });
        //电话
        $("#Phone").formValidator({ validatorgroup: this.id,
            onshow: "请输入电话", onfocus: "请输入电话", oncorrect: " "
        }).functionValidator({
            fun: function(val, elem) {
                if ($("#Phone").val() == "" && $("#Mobile").val() == "") {
                    return false;
                } else {
                    return true;
                }
            }, onerror: "请输入手机号码或电话"
        });
        //手机号码
        $("#Mobile").formValidator({ validatorgroup: this.id,
            onshow: "请输入手机号", onfocus: "请输入手机号", oncorrect: " "
        }).functionValidator({
            fun: function(val, elem) { if (val == "") return true; return val.isMobile(); }, onerror: "请输入正确的手机号"
        }).functionValidator({
            fun: function(val, elem) {
                if ($("#Phone").val() == "" && $("#Mobile").val() == "") {
                    return false;
                } else {
                    return true;
                }
            }, onerror: "请输入手机号码或电话"
        });

    },
    createForm: function() {
        return formCommon.ViewHouseForm('viewhouse');
    },
    createComboList: function() {
        var _this = this;

        //证件类型
        var certificateType = new Chenxin.UI.ParameterCombo({
            containerId: '#viewhouse_CertificateType',
            name: 'CertificateType',
            key: 'Custom.CertificateType',
            inModelType: 'txt',
            tbxWidth: 95,
            onSelected: function() {
                $("#CertificateNo").blur();
            }
        });
        //国籍
        var nationality = new Chenxin.UI.Combo({
            containerId: '#viewhouse_Nationality',
            name: 'Nationality',
            inModelType: 'txt',
            tbxWidth: 95,
            items: comboCommon.country
        });

        //带看人部门
        this.cmbviewdepart = new Chenxin.UI.StructCombo({
            param: { op: 'getcontroldeparts' },
            containerId: '#divViewDepartment',
            inModelType: 'all',
            name: 'DepartId',
            txtModelName: "ReportDepart",
            tbxWidth: 90,
            colWidth: 100,
            autoLoad: true,
            required: true,
            onSelected: function() {
                if (this.getValue() == this.defVal) {
                    _this.cmbviewuser.clearItems();
                } else {
                    _this.cmbviewuser.param.fatherid = this.getValue();
                    _this.cmbviewuser.reload();
                }
            }
        });
        //带看人
        this.cmbviewuser = new Chenxin.UI.StructCombo({
            autoLoad: false,
            param: { op: 'getemployee' },
            inModelType: 'all',
            name: 'ReportorId',
            txtModelName: "Reportor",
            tbxWidth: 90,
            colWidth: 100,
            containerId: '#divViewUser',
            required: true
        });
        return [certificateType, nationality, this.cmbviewdepart, this.cmbviewuser];
    },
    initEvent: function() {
    },
    initComponent: function() {
        Chenxin.UI.ViewHouseForm.superclass.initComponent.call(this);
    }
});

$(document).ready(function(){
	var customerMgr=new Chenxin.UI.CustomerMgr();
})